package com.liuyi.netty.im.lychat.modular.service;

import com.liuyi.netty.im.commons.codes.data.event.Message;
import com.liuyi.netty.im.commons.codes.data.event.message.playload.Playload;
import com.liuyi.netty.im.lychat.modular.entity.MessageRecords;
import com.liuyi.netty.im.lychat.modular.entity.MessageUserRecords;
import com.liuyi.netty.im.lychat.modular.enums.MessageStatus;
import com.liuyi.netty.im.lychat.modular.enums.MessageType;
import com.liuyi.netty.im.lychat.modular.enums.SessionType;
import com.liuyi.netty.im.lychat.modular.mapper.MessageUserRecordsMapper;
import com.liuyi.rapid.develop.mybatis.plus.base.IBaseService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 消息用户记录表 服务类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-12-29
 */
public interface IMessageUserRecordsService extends IBaseService<MessageUserRecords> {

    @Override
    default Class<MessageUserRecords> getEntityClass() {
        return MessageUserRecords.class;
    }

    @Override
    @SuppressWarnings("unchecked")
    default MessageUserRecordsMapper getMapper() {
        return (MessageUserRecordsMapper) getBaseMapper();
    }

    /**
     * 失去效力的消息
     * <pre>
     *     指定会话id，使该消息失效，只有未发送消息可以使用
     * </pre>
     *
     * @param userId    用户id
     * @param sessionId 会话id
     */
    default void loseEfficacyMessage(String userId, String sessionId) {
        this.update(new MessageUserRecords().setMessageStatus(MessageStatus.LOSE_EFFICACY),
                q().eq(userId != null, MessageUserRecords.USER_ID, userId)
                        .eq(MessageUserRecords.SESSION_ID, sessionId));
    }

    default void updateStatus(String userId, Long messageId, MessageStatus status) {
        this.update(new MessageUserRecords().setMessageStatus(status), q().eq(MessageUserRecords.USER_ID, userId).eq(MessageUserRecords.MESSAGE_ID, messageId));
    }

    default List<MessageUserRecords> listByUserIdAndStatus(String userId, MessageStatus status) {
        return list(q().eq(MessageUserRecords.USER_ID, userId).eq(MessageUserRecords.MESSAGE_STATUS, status));
    }


    @Transactional(rollbackFor = {Exception.class})
    default MessageRecords createSendMessage(String fromId, String toId, SessionType sessionType, MessageType contentType, Object content) {
        MessageRecords message = createMessage(fromId, toId, sessionType, contentType, content);
        sendMessage(message);
        return message;
    }

    /**
     * 创建消息
     *
     * @param fromId      从id
     * @param toId        为id
     * @param sessionType 会话类型
     * @param contentType 内容类型
     * @param content     内容
     */
    MessageRecords createMessage(String fromId, String toId, SessionType sessionType, MessageType contentType, Object content);

    void sendMessage(MessageRecords messageId);

    /**
     * 发送消息
     *
     * @param messageId 消息id
     */
    void sendMessage(Long messageId);

    /**
     * 包装信息
     *
     * @param messageUserRecords 消息的用户记录
     * @return {@link List}<{@link Message}<{@link ?} {@link extends} {@link Playload}>>
     */
    List<Message<? extends Playload>> wrapperMessage(List<MessageUserRecords> messageUserRecords);

    /**
     * 包装信息
     *
     * @param messageUserRecords 消息的用户记录
     * @return {@link List}<{@link Message}<{@link ?} {@link extends} {@link Playload}>>
     */
    Message<? extends Playload> wrapperMessage(MessageUserRecords messageUserRecords);

    Message<? extends Playload> wrapper(MessageRecords messageRecords, String fromId, String sessionId);
}
